﻿using ManagementApi.Models.Manage.ProjectCorp;
using ManagementApi.Mvc.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.Corp;
using Modules.Project;
using ManagementApi.Models;
using ManagementApi.Mvc;
using ManagementApi.Models.Manage.ProjectGroup;
using PmSoft.Entity;
using System.Security.Authentication;
using Modules.Project.Entitys;
using ManagementApi.Models.Corp;
using ManagementApi.Mvc.ApplicationContexts;

namespace ManagementApi.Controllers
{
    /// <summary>
    /// 需要选择项目的用户通用业务控制器
    /// </summary>
    [Route("/api/v1/prj_user_common")]
    [ApiController]
    [Authorize(Roles = "Corp,Manager,Commander,Constructor")]
    [TypeFilter(typeof(RequireSelProjectActionFilter))]
    public class ProjectUserCommonController : ControllerBase
    {
        /// <summary>
        /// 请求上下文
        /// </summary>
        private readonly ProjectUserApplicationContext applicationContext;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="applicationContext"></param>
        public ProjectUserCommonController(ProjectUserApplicationContext applicationContext)
        {
            this.applicationContext = applicationContext;
        }

        /// <summary>
        /// 当前项目的参建企业下拉(by:pingmac 2024-03-16)
        /// </summary>
        /// <returns></returns>
        [HttpGet("corp_pulldown")]
        public ApiResult<IEnumerable<ProjectCorpDto>> CorpsPulldown()
        {
            var projectInfo = applicationContext.CurrentProjectInfo;
            var projectCorps = applicationContext.GetService<ProjectCorpService>().GetProjectCorpsByProjectId(projectInfo.ProjectId);
            var projectCorpDtos = projectCorps.Select(source => source.AsDto(projectInfo, applicationContext)).ToList();
            var lumpContractor = applicationContext.GetService<CorpService>().GetCorpInfo(projectInfo.LumpContractorCorpId);//总包
            if (lumpContractor != null)
            {
                var projectCorpDto = lumpContractor.AsDto(projectInfo, applicationContext, "总承包");
                projectCorpDtos.Add(projectCorpDto);
            }
            if (applicationContext is CorpApplicationContext corpApplicationContext 
                && !corpApplicationContext.IsLumpContractorCorp())//非总包需要筛选企业
                projectCorpDtos = projectCorpDtos.Where(a => a.CorpId == corpApplicationContext.CurrentCorp.CorpId).ToList();

            return ApiResult<IEnumerable<ProjectCorpDto>>.Success(projectCorpDtos);
        }

        /// <summary>
        /// 当前项目的班组下拉(by:pingmac 2024-03-16)
        /// </summary>
        /// <returns></returns>
        [HttpGet("group_pulldown")]
        public ApiResult<IEnumerable<LabelValue<int>>> GroupsPulldown()
        {
            var projectGroups = applicationContext.GetService<ProjectGroupService>().GetProjectGroupsByProjectId(applicationContext.CurrentProjectInfo.ProjectId);
            if (applicationContext is CorpApplicationContext corpApplicationContext 
                && !corpApplicationContext.IsLumpContractorCorp())//非总包需要筛选企业
                projectGroups = projectGroups.Where(a => a.CorpId == corpApplicationContext.CurrentCorp.CorpId);
            var dtos = projectGroups.AsPullDownDtos();
            return ApiResult<IEnumerable<LabelValue<int>>>.Success(dtos);
        }

        /// <summary>
        /// 获取当前项目的参建企业
        /// </summary>
        /// <returns></returns>
        [HttpGet("corps_manager")]
        public ApiResult<IEnumerable<CorpInfoDto>> GetProjectCorps()
        {
            if (applicationContext is CorpApplicationContext corpApplicationContext
                  && !corpApplicationContext.IsLumpContractorCorp())//非总包需要筛选企业
            {
                List<CorpInfo> corps = new() { corpApplicationContext.CurrentCorp };
                var dtos = corps.Select(m => m.AsDto(applicationContext));
                return ApiResult<IEnumerable<CorpInfoDto>>.Success(dtos);
            }
            else
            {
                var corpInfos = applicationContext.GetService<CorpService>().GetCorpsByProjectId(applicationContext.CurrentProjectInfo.ProjectId);
                var dtos = corpInfos.Select(m => m.AsDto(applicationContext));
                return ApiResult<IEnumerable<CorpInfoDto>>.Success(dtos);
            }
        }
    }
}
